package com.lam.dormitory.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.dormitory.entity.Dormitory;
import com.lam.dormitory.entity.Student;
import com.lam.dormitory.mapper.DormitoryMapper;
import com.lam.dormitory.service.IDormitoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lam.dormitory.service.IStudentService;
import com.lam.dormitory.util.ResponseTemplate;
import com.lam.dormitory.vo.DormitoryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lam
 * @since 2019-12-26
 */
@Service
public class DormitoryServiceImpl extends ServiceImpl<DormitoryMapper, Dormitory> implements IDormitoryService {
    @Autowired
    private IStudentService studentService;

//    @Override
//    public List<Dormitory> selectAdminDorByUserId(Long userId) {
//        return this.baseMapper.selectAdminDorByUserId(userId);
//    }
//
//    @Override
//    public List<Dormitory> selectStuDorByUserId(Long userId) {
//        return this.baseMapper.selectStuDorByUserId(userId);
//    }

    @Override
    public List<DormitoryVo> selectDorByDormitory(Dormitory dormitory) {
        return this.baseMapper.selectDorByDormitory(dormitory);
    }

    @Override
    public List<Dormitory> selectBuildingNo() {
        return this.baseMapper.selectBuildingNo();
    }

    @Override
    public ResponseTemplate addAssignStudents(Long dorId, List<Long> userIds) {
        try {
            Student student = new Student();
            student.setDorId(dorId);
            for (Long userId : userIds) {
                QueryWrapper<Student> wrapper = new QueryWrapper<>();
                wrapper.lambda().eq(Student::getUserId, userId);
                studentService.update(student, wrapper);
            }
            return new ResponseTemplate(200, "分配成功");
        } catch (Exception e) {
            return new ResponseTemplate(500, "分配失败");
        }
    }
//
//    @Override
//    public Dormitory selectDorIdByBuildingNoAndDorNo(Dormitory dormitory) {
//        return this.baseMapper.selectDorIdByBuildingNoAndDorNo(dormitory);
//    }
}
